<?php

namespace api\service;

use api\components\WeChatPublic;
use Yii;

class WechatService
{
    public function userPubMin($pub_id)
    {
        $sql = "SELECT pub.id AS pub_id, pub.openid AS pub_openid, pub.nickname, pub.headimgurl, pub.unionid,mini.id AS mini_id, mini.openid AS mini_openid, mini.mall_total_fee, mini.mall_put_money, mini.mall_id, mini.mall_is FROM pub_user AS pub LEFT JOIN edu_user  AS mini ON pub.unionid = mini.unionid WHERE pub.id = {$pub_id}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    public function upload_meterial($imgRelativeSrc='default.jpg',$access_token)
    {
        $fileurl = $imgRelativeSrc;
        $type = 'image';
        //获取access_token

        //接口
        $url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $access_token . '&type=' . $type;
        //上传
        $result = $this->curl_post($url, $fileurl);
//		if(empty($result['media_id'])){
//			file_put_contents('3333'.date("Y-m-d").'.txt',$result.PHP_EOL,FILE_APPEND);
//		}
        return $result['media_id'];
    }

    private function curl_post($url, $fileurl)
    {
        $curl = curl_init();
        //这里用特性检测php版本
        if (class_exists('\CURLFile')) {
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
            //php版本大于>=5.5的
            $data = array('media' => new \CURLFile($fileurl));
        } else {
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
            }
            //php 版本小于<=5.5的
            $data = array('media' => '@' . realpath($fileurl));

        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "DEFAULT");
        $result = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl);
        return json_decode($result, TRUE);
    }

    public function replySpecial($msgData, $access_token)
    {
        $json = json_encode($msgData, JSON_UNESCAPED_UNICODE);  //php5.4+
        /*
         * POST发送https请求客服接口api
         */
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
        //以'json'格式发送post的https请求
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($json)) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl);
        if ($output == 0) {
            echo 'success';
            exit;
        }
        //$this->transfer_customer_service->reply();
    }
}